<template>
    <div class="main">
        <div class="header">
            <a href="/">
                <img src="src/assets/img/logo3.png" alt="">
            </a>
            <h1>用户注册</h1>
        </div>
        <!-- 
      表单具有默认的提交行为，默认是同步的，同步表单提交，浏览器会锁死（转圈儿）等待服务端的响应结果。
      表单的同步提交之后，无论服务端响应的是什么，都会直接把响应的结果覆盖掉当前页面。

      后来有人想到了一种办法，来解决这个问题。
     -->
        <form @submit.prevent="register">
            <div class="form-group">
                <label for="email">邮箱</label>
                <input type="email" minlength="8" maxlength="30" @input="chckEail" v-model="forData.email" class="form-control" id="email" name="email" placeholder="Email" autofocus required v-focus>
                <p class="text-danger" v-show="emailMessage">邮箱已经存在</p>
            </div>
            <div class="form-group">
                <label for="nickname">昵称</label>
                <input type="text" @input="chckNickname" v-model="forData.nickname" class="form-control" id="nickname" minlength="2" maxlength="25" name="nickname" placeholder="Nickname" required>
                <p class="text-danger" v-show="nicknameMessage">昵称已经存在</p>
            </div>
            <div class="form-group">
                <label for="password">密码</label>
                <input type="password" minlength="6" maxlength="15" v-model="forData.password" class="form-control" id="password" name="password" placeholder="Password" required>
            </div>
            <button type="submit" class="btn btn-success btn-block">注册</button>
        </form>
        <div class="message">
            <p>已有账号? <router-link to="/login">点击登录</router-link>.</p>
        </div>
    </div>
</template>
<script>
import axios from 'axios'
import lodash from 'lodash'


export default {
    data() {
        return {
            forData: {
                email: '',
                nickname: '',
                password: ''
            },
            emailMessage: false,
            nicknameMessage: false
        }
    },
    methods: {
        async register() {
            if (await this.chckEail()) {
                return window.alert('账户已经存在')
            }
            if (await this.chckNickname()) {
                return window.alert('昵称已经存在')
            }
            const ret = await axios.post('/api/users', this.forData)
            if (confirm('注册成功是否跳转页面至首页')) {
                this.$router.push('/')
            } else {

            }
        },
        chckEail: _.debounce(async function() {
            const { data } = await axios.get(`/api/users?email=${this.forData.email}`)
            const exists = data[0] ? true : false
            this.emailMessage = exists
            return exists
        }, 500),
        chckNickname: _.debounce(async function() {
            const { data } = await axios.get(`/api/users?nickname=${this.forData.nickname}`)
            const exists = data[0] ? true : false
            this.nicknameMessage = exists
            return exists
        }, 500)
    }
}
</script>
<style>
</style>